﻿using Abp.Application.Navigation;
using Abp.Localization;
using FutureApp.Navigation.Dto;
using System.Linq;

namespace FutureApp.Navigation
{
    internal class FutureAppNavigationProvider : NavigationProvider
    {
        public IMenuAppService MenuAppService { get; set; }

        public override void SetNavigation(INavigationProviderContext context)
        {
            var mainMenu = context.Manager.MainMenu;
            var menus = this.MenuAppService.GetMenus();
            menus.Items.Select(ComplexTransfer).ToList().ForEach(menu => mainMenu.AddItem(menu));
        }

        private static MenuItemDefinition ComplexTransfer(MenuEntityDto dto)
        {
            var menuRoot = Transfer(dto);
            foreach (var child in dto.Menus)
            {
                menuRoot.AddItem(Transfer(child));
            }
            return menuRoot;
        }

        private static MenuItemDefinition Transfer(MenuEntityDto dto)
        {
            return new MenuItemDefinition(
                name: dto.Code,
                displayName: L(dto.Name),
                icon: dto.Icon,
                url: dto.NavigationUrl,
                requiresAuthentication: dto.RequiresAuthentication,
                requiredPermissionName: dto.RequiredPermission,
                order: dto.Order
            );
        }

        private static ILocalizableString L(string name)
        {
            return new LocalizableString(name, FutureAppConsts.LocalizationSourceName);
        }
    }
}
